package databasetest;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.lang.reflect.Array;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Arrays;
import java.util.Vector;

public class Update extends JInternalFrame {

	Connection conn;
	DatabaseTest dt;
	Vector<String> vc = new Vector<String>();

	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;
	public JTextField code;
	public JTextField ea;
	private JButton btnNewButton;
	private JButton btnNewButton_1;
	private JLabel lblNewLabel_2;
	public JTextField serial;
	private JLabel status;
	private JButton btnNewButton_2;

	
	public Update(Connection c,DatabaseTest dt ) {
		super("수정", true, true, true, false);
		setBounds(500, 50, 450, 300);
		getContentPane().setLayout(null);
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getLblNewLabel_1());
		getContentPane().add(getCode());
		getContentPane().add(getEa());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getBtnNewButton_1());
		getContentPane().add(getLblNewLabel_2());
		getContentPane().add(getTextField_2());
		getContentPane().add(getStatus());
		getContentPane().add(getBtnNewButton_2());
		conn = c;
		this.dt=dt;
	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("code");
			lblNewLabel.setBounds(41, 92, 57, 15);
		}
		return lblNewLabel;
	}

	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("ea");
			lblNewLabel_1.setBounds(41, 148, 57, 15);
		}
		return lblNewLabel_1;
	}

	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setBounds(131, 89, 116, 21);
			code.setColumns(10);
		}
		return code;
	}

	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setBounds(131, 145, 116, 21);
			ea.setColumns(10);
		}
		return ea;
	}

	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("수정");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {

					int s = Integer.parseInt(serial.getText()); // 시리얼
					String c = code.getText(); // 코드
					int e1 = Integer.parseInt(ea.getText()); // 개수

					String sql = "update ptbl set code=?, ea=?"
							+ "where serial=?";

					try {

						PreparedStatement pstmt = conn.prepareStatement(sql);
						pstmt.setString(1, c);
						pstmt.setInt(2, e1);
						pstmt.setInt(3, s);

						pstmt.executeUpdate();
						pstmt.close();
						
						if(dt.select != null)  dt.select.setList();	//조회목록 갱신

					} catch (Exception ex) {
						ex.printStackTrace();
					}

				}
			});
			btnNewButton.setBounds(82, 193, 97, 23);
		}
		return btnNewButton;
	}

	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("검색"); // //////////////
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int s = Integer.parseInt(serial.getText());

					String sql = "select*from ptbl where serial = ?";

					try {

						PreparedStatement pstmt = conn.prepareStatement(sql);
						pstmt.setInt(1, s);
						// select한 여러개의 값들을...

						ResultSet rs = pstmt.executeQuery(); // 시리얼에 맞는 코드,개수를
																// 가져옴
						if (rs.next()) {
							code.setText(rs.getString("code"));
							ea.setText("" + rs.getInt("ea"));

						} else {
							status.setText("데이터가 없습니다.");
							code.setText("");
							ea.setText("");
							serial.requestFocus();
						}
						rs.close();
						pstmt.close();

					} catch (Exception ex) {
						ex.printStackTrace();
					}

				}
			});
			btnNewButton_1.setBounds(291, 36, 97, 23);
		}
		return btnNewButton_1;
	}

	private JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			lblNewLabel_2 = new JLabel("serial");
			lblNewLabel_2.setBounds(41, 40, 57, 15);
		}
		return lblNewLabel_2;
	}

	private JTextField getTextField_2() {
		if (serial == null) {
			serial = new JTextField();
			serial.setBounds(131, 37, 116, 21);
			serial.setColumns(10);
		}
		return serial;
	}

	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel("New label");
			status.setBounds(41, 226, 347, 15);
		}
		return status;
	}

	
	public void setData(String s,String c, String e) {

		System.out.println(s+""+c+""+e);
		
		this.serial.setText(s);
		this.code.setText(c);
		this.ea.setText(e);

	}
	
	private JButton getBtnNewButton_2() {
		if (btnNewButton_2 == null) {
			btnNewButton_2 = new JButton("삭제");
			btnNewButton_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {					
				
					int s = Integer.parseInt(serial.getText()) ;					
					String sql="delete from ptbl where serial=?";					
					try{						
						PreparedStatement pstmt=conn.prepareStatement(sql);						
						pstmt.setInt(1, s);						
						pstmt.executeUpdate();
						pstmt.close();						
						if(dt.select != null)  dt.select.setList();	//조회 목록 갱신
					}catch(Exception ex){
						ex.printStackTrace();
					}
				}
			});
			btnNewButton_2.setBounds(209, 193, 97, 23);
		}
		return btnNewButton_2;
	}
}
